using System;
using System.Threading.Tasks;
using BanZuiCloudControlAgent.Core.Tasks.Base;
using BanZuiCloudControlAgent.Core.Tasks.Models;
using BanZuiCloudControlAgent.Utils.Common;

namespace BanZuiCloudControlAgent.Core.Tasks.Specific.System
{
    /// <summary>
    /// 配置任务，用于处理系统配置更新
    /// </summary>
    public class ConfigTask : BaseTask
    {
        /// <summary>
        /// 执行配置任务
        /// </summary>
        /// <param name="context">任务上下文</param>
        /// <returns>任务结果</returns>
        public override async Task<TaskResult> ExecuteAsync(TaskContext context)
        {
            try
            {
                SetTaskStarted();
                _logger.Info($"开始执行配置任务: {TaskId}");
                
                // 获取参数
                int statusReportInterval = GetParameter<int>("statusReportInterval");
                int idleStatusReportInterval = GetParameter<int>("idleStatusReportInterval");
                int offlineThreshold = GetParameter<int>("offlineThreshold");
                string logLevel = GetParameter<string>("logLevel");
                int logUploadInterval = GetParameter<int>("logUploadInterval");
                bool enableAutoUpdate = GetParameter<bool>("enableAutoUpdate");
                int autoUpdateCheckInterval = GetParameter<int>("autoUpdateCheckInterval");
                
                _logger.Info($"配置任务参数: 状态上报间隔={statusReportInterval}, 空闲状态上报间隔={idleStatusReportInterval}, 离线阈值={offlineThreshold}");
                
                // 更新进度
                UpdateProgress(20);
                
                // 更新配置
                if (statusReportInterval > 0)
                {
                    ConfigurationManager.SetValue("UI:HeartbeatInterval", statusReportInterval);
                    _logger.Info($"已更新心跳间隔: {statusReportInterval}秒");
                }
                
                UpdateProgress(40);
                
                if (idleStatusReportInterval > 0)
                {
                    ConfigurationManager.SetValue("UI:IdleStatusReportInterval", idleStatusReportInterval);
                    _logger.Info($"已更新空闲状态上报间隔: {idleStatusReportInterval}秒");
                }
                
                UpdateProgress(60);
                
                if (offlineThreshold > 0)
                {
                    ConfigurationManager.SetValue("UI:OfflineThreshold", offlineThreshold);
                    _logger.Info($"已更新离线阈值: {offlineThreshold}秒");
                }
                
                UpdateProgress(80);
                
                if (!string.IsNullOrEmpty(logLevel))
                {
                    ConfigurationManager.SetValue("Logging:LogLevel", logLevel);
                    _logger.Info($"已更新日志级别: {logLevel}");
                }
                
                if (logUploadInterval > 0)
                {
                    ConfigurationManager.SetValue("Logging:UploadInterval", logUploadInterval);
                    _logger.Info($"已更新日志上传间隔: {logUploadInterval}秒");
                }
                
                ConfigurationManager.SetValue("Update:EnableAutoUpdate", enableAutoUpdate);
                _logger.Info($"已更新自动更新设置: {enableAutoUpdate}");
                
                if (autoUpdateCheckInterval > 0)
                {
                    ConfigurationManager.SetValue("Update:CheckInterval", autoUpdateCheckInterval);
                    _logger.Info($"已更新自动更新检查间隔: {autoUpdateCheckInterval}秒");
                }
                
                // 保存配置
                ConfigurationManager.SaveConfig();
                
                UpdateProgress(100);
                
                // 返回成功结果
                var result = TaskResult.Success("配置更新成功");
                SetTaskCompleted(result);
                return result;
            }
            catch (Exception ex)
            {
                _logger.Error( $"执行配置任务时发生异常: {ex.Message}", ex);
                var result = TaskResult.Failure("执行配置任务时发生异常", ex);
                SetTaskCompleted(result);
                return result;
            }
        }
    }
} 